{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-03-03T07:19:18.651426Z",
     "start_time": "2025-03-03T07:19:18.458221Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T07:26:38.506317Z",
     "start_time": "2025-03-03T07:26:38.497258Z"
    }
   },
   "cell_type": "code",
   "source": [
    "dict_obj = {\n",
    "    'key1': ['a', 'b', 'a', 'b', 'a','b','a','a'],\n",
    "    'key2': ['one','one','two','three','two','two','one','three'],\n",
    "    'data1':np.random.randint(1,10,8),\n",
    "    'data2':np.random.randint(1,10,8)\n",
    "}\n",
    "df_obj = pd.DataFrame(dict_obj)\n",
    "print(df_obj)\n",
    "print('-'*50)"
   ],
   "id": "68605ef309e989fb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  key1   key2  data1  data2\n",
      "0    a    one      8      8\n",
      "1    b    one      4      8\n",
      "2    a    two      9      1\n",
      "3    b  three      2      6\n",
      "4    a    two      2      1\n",
      "5    b    two      1      3\n",
      "6    a    one      1      7\n",
      "7    a  three      8      2\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T07:34:54.298336Z",
     "start_time": "2025-03-03T07:34:54.288510Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 方法一:\n",
    "k1_sum = df_obj.groupby('key1').mean(numeric_only=True).add_prefix('mean_')  # groupby()用于分组，mean()用于求均值  .add_prefix()用于给列名加前缀\n",
    "print(k1_sum)\n",
    "print('-'*50)"
   ],
   "id": "3e6e2608c794a7bd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      mean_data1  mean_data2\n",
      "key1                        \n",
      "a       5.600000    3.800000\n",
      "b       2.333333    5.666667\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T07:38:10.707399Z",
     "start_time": "2025-03-03T07:38:10.702581Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df_obj.groupby('key1').mean(numeric_only=True))  # 与上面的区别就是不加前缀\n",
    "print('-'*50)"
   ],
   "id": "39d14d409a673723",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         data1     data2\n",
      "key1                    \n",
      "a     5.600000  3.800000\n",
      "b     2.333333  5.666667\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T08:15:37.122150Z",
     "start_time": "2025-03-03T08:15:37.116660Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 方法2：\n",
    "k1_sum_tf = df_obj.groupby('key1').transform('mean', numeric_only=True).add_prefix('mean_')  # transform()用于对分组后的列进行操作\n",
    "print(k1_sum_tf)  # 但是这种方法的输出格式与方法一的不同,transform会保持原来的格式\n",
    "print('-'*50)"
   ],
   "id": "65f112bc552dc1ce",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   mean_data1  mean_data2\n",
      "0    5.600000    3.800000\n",
      "1    2.333333    5.666667\n",
      "2    5.600000    3.800000\n",
      "3    2.333333    5.666667\n",
      "4    5.600000    3.800000\n",
      "5    2.333333    5.666667\n",
      "6    5.600000    3.800000\n",
      "7    5.600000    3.800000\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T08:11:36.557405Z",
     "start_time": "2025-03-03T08:11:36.549766Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def diff_mean(s):\n",
    "    \"\"\"\n",
    "    返回数据与均值的差值，s传入的是某一个分组\n",
    "    :param s: \n",
    "    :return: \n",
    "    \"\"\"\n",
    "    return s - s.mean()\n",
    "print(df_obj.loc[:,['key1','data1','data2']].groupby('key1').transform(diff_mean))"
   ],
   "id": "55e5d0997e71c99",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      data1     data2\n",
      "0  2.400000  4.200000\n",
      "1  1.666667  2.333333\n",
      "2  3.400000 -2.800000\n",
      "3 -0.333333  0.333333\n",
      "4 -3.600000 -2.800000\n",
      "5 -1.333333 -2.666667\n",
      "6 -4.600000  3.200000\n",
      "7  2.400000 -1.800000\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T08:23:49.283933Z",
     "start_time": "2025-03-03T08:23:49.279729Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df_obj = pd.DataFrame({'data1':['a']*4+['b']*4,\n",
    "                       'data2':np.random.randint(0,4,8)})\n",
    "print(df_obj)"
   ],
   "id": "a98641ba27a4d05d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  data1  data2\n",
      "0     a      3\n",
      "1     a      1\n",
      "2     a      2\n",
      "3     a      2\n",
      "4     b      0\n",
      "5     b      2\n",
      "6     b      0\n",
      "7     b      0\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T08:25:24.757394Z",
     "start_time": "2025-03-03T08:25:24.752211Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df_obj.duplicated())\n",
    "\"\"\"\n",
    "DataFrame.duplicated(subset=None, keep='first')\n",
    "参数解释\n",
    "subset：可选参数，是一个列名列表，用于指定在哪些列中检查重复项。如果不指定，则会考虑所有列。\n",
    "keep：用于指定标记重复值的方式，有以下三个可选值：\n",
    "'first'（默认值）：将重复值中第一次出现的元素标记为 False，其余重复元素标记为 True。\n",
    "'last'：将重复值中最后一次出现的元素标记为 False，其余重复元素标记为 True。\n",
    "False：将所有重复元素都标记为 True。\n",
    "\"\"\""
   ],
   "id": "daae62d1eae5a236",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    False\n",
      "1    False\n",
      "2    False\n",
      "3     True\n",
      "4    False\n",
      "5    False\n",
      "6     True\n",
      "7     True\n",
      "dtype: bool\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T08:39:09.335267Z",
     "start_time": "2025-03-03T08:39:09.331123Z"
    }
   },
   "cell_type": "code",
   "source": "print(df_obj[~df_obj.duplicated()])  # 取出不重复的行",
   "id": "1a866f06a4f45e4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  data1  data2\n",
      "0     a      3\n",
      "1     a      1\n",
      "2     a      2\n",
      "4     b      0\n",
      "5     b      2\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T08:40:10.903576Z",
     "start_time": "2025-03-03T08:40:10.899880Z"
    }
   },
   "cell_type": "code",
   "source": "print(df_obj.duplicated('data2'))  # 也可以指定列名来检查重复项",
   "id": "abba21f8c62ddbb0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    False\n",
      "1    False\n",
      "2    False\n",
      "3     True\n",
      "4    False\n",
      "5     True\n",
      "6     True\n",
      "7     True\n",
      "dtype: bool\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T08:44:48.269782Z",
     "start_time": "2025-03-03T08:44:48.265709Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df_obj1 = pd.DataFrame({'data1':[np.nan]*4,\n",
    "                        'data2':list('1225')}\n",
    "                       )\n",
    "print(df_obj1)"
   ],
   "id": "c202302664ab5783",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   data1 data2\n",
      "0    NaN     1\n",
      "1    NaN     2\n",
      "2    NaN     2\n",
      "3    NaN     5\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T08:46:16.995445Z",
     "start_time": "2025-03-03T08:46:16.990624Z"
    }
   },
   "cell_type": "code",
   "source": "print(df_obj1.duplicated('data1'))",
   "id": "59241fd552eaa764",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    False\n",
      "1     True\n",
      "2     True\n",
      "3     True\n",
      "dtype: bool\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T08:47:01.625995Z",
     "start_time": "2025-03-03T08:47:01.622202Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\"\"\"\n",
    "rop_duplicates() 是 Pandas 库中用于去除 DataFrame 或 Series 里重复项的重要方法。\n",
    "DataFrame.drop_duplicates(subset=None, keep='first', inplace=False, ignore_index=False)\n",
    "参数解释\n",
    "subset：可选参数，是一个列名列表，用于指定在哪些列中检查重复项。若不指定，默认会考虑所有列。例如，当你只想依据某几列的值来判断行是否重复时，就可以通过该参数指定这些列。\n",
    "keep：用于指定保留重复行的方式，有以下三个可选值：\n",
    "'first'（默认值）：保留重复行中第一次出现的行，删除其余重复行。\n",
    "'last'：保留重复行中最后一次出现的行，删除其余重复行。\n",
    "False：删除所有重复行，即只要存在重复，所有相关行都被删除。\n",
    "inplace：布尔类型参数，默认为 False。若为 True，会直接在原 DataFrame 上进行修改，不返回新对象；若为 False，则返回一个去除重复项后的新 DataFrame。\n",
    "ignore_index：布尔类型参数，默认为 False。若为 True，会对返回的 DataFrame 进行索引重置，生成从 0 开始的连续整数索引；若为 False，则保留原索引。\n",
    "\"\"\"\n",
    "print(df_obj1.drop_duplicates('data1'))"
   ],
   "id": "eb1cffcc47245e7c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   data1 data2\n",
      "0    NaN     1\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T08:49:45.676520Z",
     "start_time": "2025-03-03T08:49:45.671687Z"
    }
   },
   "cell_type": "code",
   "source": "print(df_obj1.drop_duplicates())  # 不指定列名，默认会考虑所有列",
   "id": "c3854e8f4d1a5bdd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   data1 data2\n",
      "0    NaN     1\n",
      "1    NaN     2\n",
      "3    NaN     5\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "b72a035135939d5d"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
